<?php


namespace App\Model\Logic;


use App\Helper\RedisConst;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Redis\Pool;

/**
 *
 * @Bean()
 * Class LimitNumCheckLogic
 * @package App\Model\Logic
 */
class LimitNumCheckLogic
{

    /**
     * @Inject()
     * @var Pool
     */
    private $redis;

    /**
     * --------------------------------------------------------------------------
     * 每天单设备注册用户限制数量
     * --------------------------------------------------------------------------
     * @param $serial_number
     * @param $nickname
     * @return int
     */
    public function deviceRegisterLimit($serial_number)
    {
        $key = 'deviceRegisterLimit:'.date('Ymd').sha1($serial_number);
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.device_register_limit')) return 10144;
    }


    /**
     * --------------------------------------------------------------------------
     * 每天ip注册用户限制数量
     * --------------------------------------------------------------------------
     * @param $nickname
     * @return int
     */
    public function dailyIpRegisterLimit($ip)
    {
        $key = 'dailyIpRegisterLimit:'.date('Ymd').sha1($ip);
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.daily_ip_register_limit')) return 10145;
    }

    /**
     * --------------------------------------------------------------------------
     * 单账户发帖数每日最多100个
     * --------------------------------------------------------------------------
     * @param $member_id
     * @return int
     */
    public function dailyUserPubPostLimit($member_id)
    {
        $key = 'dailyUserPubPostLimit:'.date('Ymd').$member_id;
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.daily_user_pub_post_limit')) return 10146;
    }

    /**
     * --------------------------------------------------------------------------
     * 单设备发帖数每日最多100个
     * --------------------------------------------------------------------------
     * @param $serial_number
     * @return int
     */
    public function dailyDevicePubPostLimit($serial_number)
    {
        $key = 'dailyDevicePubPostLimit:'.date('Ymd'). sha1($serial_number);
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.daily_device_pub_post_limit')) return 10146;
    }

    /**
     * --------------------------------------------------------------------------
     * 单账户发评论限制
     * --------------------------------------------------------------------------
     * @param $member_id
     * @return int
     */
    public function dailyUserPubCommentLimit($member_id)
    {
        $key = 'dailyUserPubCommentLimit:'.date('Ymd').$member_id;
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.daily_user_pub_comment_limit')) return 10147;
    }

    /**
     * --------------------------------------------------------------------------
     * 单设备发评论限制
     * --------------------------------------------------------------------------
     * @param $serial_number
     * @return int
     */
    public function dailyDevicePubCommentLimit($serial_number)
    {
        $key = 'dailyDevicePubCommentLimit:'.date('Ymd').sha1($serial_number);
        $num = $this->redis->incr($key);
        $this->redis->expire($key, RedisConst::NORMAL_EXPIRE);
        if($num > config('app.daily_device_pub_comment_limit')) return 10147;
    }

    public function userDeviceDailyBind($serial_number, $login, $lang ='en')
    {
        if(!is_numeric($login)) return;
        $key = 'userLoginDevice:'.date('Ymd').sha1($serial_number);
        $logins = $this->redis->sMembers($key);
        if(in_array($login, $logins)) return;
        if(!in_array($login, $logins) && count($logins) < config('app.device_login_user_limit')){
            $this->redis->sadd($key, $login, RedisConst::NORMAL_EXPIRE);
        } else {
            return 10149;
        }

    }
}